<?php

namespace App\Models\Goods;
use App\Models\BaseModel;
use App\Params\Backend\GoodsListParams;
use App\Params\Backend\GoodsUpdateParam;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * 商品模型类
 *
 * @author WangBin
 * @date 2021-09-12
 * @package App\Http\Models\Goods
 */
class Goods extends BaseModel
{
    protected $table = 'b2c_goods';
    protected $primaryKey = 'goods_id';
    protected $casts = [
        'tag_ids' => 'array',
        'gallery' => 'array',
        'keywords' => 'array',
        'platform' => 'json',
        'category_ids' => 'array',
    ];

    /**
     * 根据ID查询货品
     *
     * @param int $goodsId
     * @param array $columns
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getById(int $goodsId, array $columns = [])
    {
        // 合并查询字段
        $columns = $columns ? array_merge($columns, ['a.platform', 'a.keywords', 'b.name as category_name', 'a.goods_type']) : ['a.*',  'b.name as category_name'];
        return DB::table('b2c_goods as a')
            ->leftJoin('b2c_goods_category as b', 'a.category_id', '=', 'b.category_id')
            ->select($columns)
            ->where('goods_id', $goodsId)
            ->first();
    }

    /**
     * 获取商品列表
     *
     * @param GoodsListParams $params
     * @return \Illuminate\Database\Query\Builder
     */
    public static function getByFilter(GoodsListParams $params)
    {
        $goods = DB::table('b2c_goods as a')
            ->leftJoin('b2c_goods_category as b', 'a.category_id', '=', 'b.category_id')
            ->select(['a.*', 'b.name as category_name'])
            ->where('is_deleted', 0);
        if($params->getGoodsBn()) {
            $goods->where('goods_bn', 'like', '%' . $params->getGoodsBn() . '%');
        }
        if($params->getName()) {
            $goods->where('name', 'like', '%' . $params->getName() . '%');
        }
        $goods->orderByDesc('created_at');
        return $goods;
    }

    /**
     * 校验商品名称是否存在
     *
     * @param string $name
     * @param int $goodsId
     * @return bool
     */
    public static function checkName(string $name, int $goodsId = 0)
    {
        $goods = Goods::query()->where('name', $name);
        if(!empty($goodsId)) {
            $goods->where('goods_id', '!=', $goodsId);
        }
        return $goods->exists();
    }

    /**
     * 校验商品编号是否存在
     *
     * @param string $goodsBn
     * @param int $goodsId
     * @return bool
     */
    public static function checkBn(string $goodsBn, int $goodsId = 0)
    {
        $goods = Goods::query()->where('goods_bn', $goodsBn);
        if(!empty($goodsId)) {
            $goods->where('goods_id', '!=', $goodsId);
        }
        return $goods->exists();
    }

    /**
     * 根据货品ID检查是否存在
     *
     * @param int $goodsId
     * @return bool
     */
    public static function exist(int $goodsId)
    {
        return Goods::query()->where('goods_id', $goodsId)->exists();
    }

    /**
     * (H5) 端商品列表
     *
     * @param \App\Params\H5\GoodsListParams $params
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public static function getList(\App\Params\H5\GoodsListParams $params)
    {
        $goods = Goods::query();
        if($params->getCategoryId()) {
            $goods->where('category_id', $params->getCategoryId());
        }
        if($params->getKeywords()) {
            $goods->where(
                ['name', 'like',  '%' . $params->getKeywords() . '%'],
                ['keywords', 'like',  '%' . $params->getKeywords() . '%']
            );
        }
        return $goods;
    }
}
